home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / PowerPlant / CTriangleControl / CTriangleControl.cp next >
Encoding:
Text File  |  1996-09-14  |  9.5 KB  |  333 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    File:                CTriangleControl.cp
  3. // Version:            1.1.2 - May 23,1996
  4. //    Author:            Mike Shields (mshields@inconnect.com)
  5. //
  6. //    Copyright ©1994-1996 Mike Shields. All rights reserved.
  7. //    I hereby grant users of CTriangleControl permission to use it (or any modified 
  8. //    version of it) in applications (or any other type of Macintosh software 
  9. //    like extensions -- freeware, shareware, commercial, or other) for free, 
  10. //    subject to the terms that:
  11. //
  12. //        (1)  This agreement is non-exclusive.
  13. //
  14. //        (2)  I, Mike Shields, retain the copyright to the original source code.
  15. //
  16. //    These two items are the only required conditions for use. However, I do have 
  17. //    an additional request. Note, however, that this is only a request, and 
  18. //    that it is not a required condition for use of this code.
  19. //
  20. //        (1) That I be given credit for CTriangleControl code in the copyrights or 
  21. //            acknowledgements section of your manual or other appropriate documentation.
  22. //
  23. //
  24. //    I would like to repeat that this last item is only a request. You are prefectly 
  25. //    free to choose not to do any or all of them.
  26. //    
  27. //        This source code is distributed in the hope that it will be useful,
  28. //        but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. //        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  30. // ===========================================================================
  31. //    CTriangleControl.h        <- double-click + Command-D to see class declaration
  32. //
  33. //
  34. // CTriangleControl is my implementation of the rotating triangle thingy you see in the Finder
  35. // for opening folders in list views.
  36.  
  37. // Improvements coming soon:
  38. // Add in the ability to set the hilite and standard colors from a 'PPob' resource and 
  39. // programmatically. This way you can have colors other than the blues like the finder.
  40.  
  41. #include "CTriangleControl.h"
  42.  
  43. #include <LStream.h>
  44. #include "UDrawingState.More.h"
  45. #include <UDrawingState.h>
  46. #include <UEnvironment.h>
  47.  
  48. #include <String_Utils.h>
  49. #include <PP_Messages.h>
  50.  
  51. #ifndef __MEMORY__
  52. #include <Memory.h>
  53. #endif
  54.  
  55. const RGBColor cBlue = { 0xCCCC, 0xCCCC, 0xFFFF };
  56. const RGBColor cDarkBlue = { 0x6666, 0x6666, 0xFFFF };
  57.  
  58. // ---------------------------------------------------------------------------
  59. //        • CreateFromStream
  60. // ---------------------------------------------------------------------------
  61. //    Create a new Triangle object from the data in a Stream
  62.  
  63. CTriangleControl *CTriangleControl::CreateFromStream(LStream *inStream)
  64. {
  65.     return (new CTriangleControl(inStream));
  66. }
  67.  
  68. // ---------------------------------------------------------------------------
  69. //        • CTriangleControl
  70. // ---------------------------------------------------------------------------
  71. //    Default Constructor
  72.  
  73. CTriangleControl::CTriangleControl()
  74. {
  75.     mMaxValue = 1;
  76.     InitTriangleControl();
  77. }
  78.  
  79.  
  80. // ---------------------------------------------------------------------------
  81. //        • CTriangleControl(const CTriangleControl&)
  82. // ---------------------------------------------------------------------------
  83. //    Copy Constructor
  84.  
  85. CTriangleControl::CTriangleControl(const CTriangleControl &inOriginal)
  86.         : LControl(inOriginal)
  87. {
  88.     InitTriangleControl();
  89. }
  90.  
  91.  
  92. // ---------------------------------------------------------------------------
  93. //        • CTriangleControl(SPaneInfo&, MessageT, Int32, Int32, Int32)
  94. // ---------------------------------------------------------------------------
  95. //    Construct from input parameters
  96.  
  97. CTriangleControl::CTriangleControl(const SPaneInfo &inPaneInfo, MessageT inValueMessage,
  98.                                     Int32 inValue, Int32 inMinValue, Int32 inMaxValue)
  99.         : LControl(inPaneInfo, inValueMessage, inValue, inMinValue, inMaxValue)
  100. {
  101.     InitTriangleControl();
  102. }
  103.  
  104.  
  105. // ---------------------------------------------------------------------------
  106. //        • CTriangleControl(LStream*)
  107. // ---------------------------------------------------------------------------
  108. //    Construct from a data stream
  109.  
  110. CTriangleControl::CTriangleControl(LStream *inStream)
  111.         : LControl(inStream)
  112. {
  113.     InitTriangleControl();
  114. }
  115.  
  116.  
  117. // ---------------------------------------------------------------------------
  118. //        • InitTriangleControl
  119. // ---------------------------------------------------------------------------
  120. //    Private initializer
  121.  
  122. void CTriangleControl::InitTriangleControl()
  123. {
  124.     // our tringle will always be drawn at fixed coords from the frame.
  125.     // We use a polygon because I wanted a self contained object. 
  126.     // I didn't want to rely on icons in the resource file to be loaded 
  127.     // or anything like that.
  128.     mTriangleToDraw = ::OpenPoly();
  129.     if ( mValue == 0 )
  130.     {
  131.         // construct the "closed" triangle
  132.         ::MoveTo(3, 0);
  133.         ::LineTo(3, 10);
  134.         ::LineTo(8, 5);
  135.         ::LineTo(3, 0);
  136.     }
  137.     else
  138.     {
  139.         // construct the "open" triangle
  140.         ::MoveTo(0, 3);
  141.         ::LineTo(10, 3);
  142.         ::LineTo(5, 8);
  143.         ::LineTo(0, 3);
  144.     }
  145.     ::ClosePoly();
  146. }
  147.     
  148. // ---------------------------------------------------------------------------
  149. //        • ~CTriangleControl
  150. // ---------------------------------------------------------------------------
  151. //    Destructor
  152.  
  153. CTriangleControl::~CTriangleControl()
  154. {
  155.     ::KillPoly(mTriangleToDraw);
  156. }
  157.  
  158. // ---------------------------------------------------------------------------
  159. //        • SetValue
  160. // ---------------------------------------------------------------------------
  161. //    Specify the value for a Control
  162. //
  163. void CTriangleControl::SetValue(Int32 inValue)
  164. {
  165.    Boolean changed = (inValue != mValue);
  166.  
  167.     LControl::SetValue(inValue);
  168.     
  169.     if ( changed && FocusDraw() )
  170.     {
  171.         Rect    frame;
  172.         CalcLocalFrameRect(frame);
  173.         
  174.         // Get rid of the current triangle
  175.         ::KillPoly(mTriangleToDraw);    
  176.             
  177.         // make intermediate triangle
  178.         mTriangleToDraw = ::OpenPoly();
  179.         ::MoveTo(0, 8);
  180.         ::LineTo(8, 8);
  181.         ::LineTo(8, 0);
  182.         ::LineTo(0, 8);    
  183.         ::ClosePoly();
  184.         
  185.         ApplyForeAndBackColors();
  186.         ::EraseRect(&frame);
  187.         DrawTriangle(true);    
  188.         
  189.         // wait a few ticks to finish the animation
  190.         long finalTick;
  191.         ::Delay(4, &finalTick);
  192.         
  193.         // make final triangle
  194.         ::KillPoly(mTriangleToDraw);    
  195.  
  196.         mTriangleToDraw = ::OpenPoly();
  197.         if ( mValue == 1 )
  198.         {
  199.             // make the "unopened" triangle
  200.             ::MoveTo(3, 0);
  201.             ::LineTo(3, 10);
  202.             ::LineTo(8, 5);
  203.             ::LineTo(3, 0);
  204.         }
  205.         else
  206.         {
  207.             // make the "opened" triangle
  208.             ::MoveTo(0, 3);
  209.             ::LineTo(10, 3);
  210.             ::LineTo(5, 8);
  211.             ::LineTo(0, 3);
  212.         }
  213.         ::ClosePoly();
  214.  
  215.         // draw the final triangle
  216.         ApplyForeAndBackColors();
  217.         ::EraseRect(&frame);
  218.         DrawTriangle(false);
  219.     }
  220. }
  221.  
  222. // ---------------------------------------------------------------------------
  223. //        • FindHotSpot
  224. // ---------------------------------------------------------------------------
  225. //    Determine which hot spot, if any, contains the specified point
  226. //
  227. Int16 CTriangleControl::FindHotSpot(Point /*inPoint*/)
  228. {
  229.     // the whole frame is the one and only hot spot
  230.     return 1;
  231. }
  232.  
  233. // ---------------------------------------------------------------------------
  234. //        • PointInHotSpot
  235. // ---------------------------------------------------------------------------
  236. //    Determine if a point is within a specified hot spot
  237. //
  238. Boolean CTriangleControl::PointInHotSpot(Point inPoint, Int16 /*inHotSpot*/)
  239. {
  240.     Point    portPt = inPoint;
  241.     LocalToPortPoint(portPt);
  242.  
  243.     // the whole frame is the hot spot
  244.     return PointIsInFrame(portPt.h, portPt.v);
  245. }
  246.  
  247. // ---------------------------------------------------------------------------
  248. //        • HotSpotAction
  249. // ---------------------------------------------------------------------------
  250. //    Take action during mouse down tracking
  251. //
  252. void CTriangleControl::HotSpotAction(Int16 /*inHotSpot*/, Boolean inCurrInside, Boolean inPrevInside)
  253. {
  254.     // if we have moved in our out of our frame from where we were previously, then we
  255.     // need to redraw ourselves to reflect the state change
  256.     if ( inCurrInside != inPrevInside )
  257.     {        
  258.         if ( FocusDraw() )
  259.         {
  260.             DrawTriangle(inCurrInside);
  261.         }
  262.     }        
  263. }
  264.  
  265. // ---------------------------------------------------------------------------
  266. //        • HotSpotResult
  267. // ---------------------------------------------------------------------------
  268. //    Perform result of clicking and releasing mouse inside a HotSpot
  269. //
  270. void CTriangleControl::HotSpotResult(Int16 /*inHotSpot*/)
  271. {
  272.     // set the value to our opposite
  273.     SetValue((mValue ? 0 : 1 ));
  274. }
  275.  
  276. // ---------------------------------------------------------------------------
  277. //        • DrawSelf
  278. // ---------------------------------------------------------------------------
  279. //    Draw the Triangle
  280. //
  281. void CTriangleControl::DrawSelf()
  282. {
  283.     DrawTriangle(false);
  284. }
  285.  
  286. // ---------------------------------------------------------------------------
  287. //        • DrawTriangle
  288. // ---------------------------------------------------------------------------
  289. //    Draw the Triangle
  290. //
  291. void CTriangleControl::DrawTriangle(Boolean hilite)
  292. {
  293.     if ( mTriangleToDraw != NULL ) 
  294.     {
  295.         Rect    frame;
  296.         if ( CalcLocalFrameRect(frame) ) 
  297.         {    
  298.             // set us up to draw. Make sure we draw in the correct spot in relation to 
  299.             // where the frame is right now.  Restore the polygon after we are done.
  300.             ::OffsetPoly(mTriangleToDraw, frame.left, frame.top);
  301.             
  302.             StPenState    thePenState;
  303.             StPenState::Normalize();
  304.             
  305.             // We've got color, so draw the "blue" triangle
  306.             if ( UEnvironment::HasFeature(env_SupportsColor) )
  307.             {
  308.                 StColorState    theColorState;
  309.                 StColorState::Normalize();
  310.  
  311.                 // we fill with a different color depending on the hilite state
  312.                 ::RGBForeColor((hilite ? &cDarkBlue : &cBlue));
  313.                 ::FillPoly(mTriangleToDraw, &(UQDGlobals::GetQDGlobals()->black));
  314.         
  315.                 StColorState::Normalize();
  316.                 ::FramePoly(mTriangleToDraw);
  317.             }
  318.             else
  319.             {
  320.                 if ( hilite )
  321.                 {
  322.                     ::FillPoly(mTriangleToDraw, &(UQDGlobals::GetQDGlobals()->black));
  323.                 }
  324.                 ::FramePoly(mTriangleToDraw);
  325.             }
  326.             
  327.             // restore the polygon
  328.             ::OffsetPoly(mTriangleToDraw, -frame.left, -frame.top);
  329.         }
  330.     }
  331. }
  332.  
  333.